⚡️ Speed up method TextSplitter.prompt_template_token_length
by 5%
#57
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 5% (0.05x) speedup for
TextSplitter.prompt_template_token_length
inguardrails/utils/docs_utils.py
⏱️ Runtime :
13.7 microseconds
→13.0 microseconds
(best of24
runs)📝 Explanation and details
The optimization introduces a caching mechanism that eliminates redundant template variable parsing.
Key changes:
variable_names
are computed once during initialization usingget_template_variables()
and stored as an instance variable, rather than being recalculated on every call toget_prompt_variables()
.get_template_variables(self.source)
each time, the method now directly uses the cachedself.variable_names
.Why this improves performance:
Template variable extraction involves parsing the template string to identify placeholder variables (e.g.,
${variable_name}
). This parsing operation has computational overhead that scales with template complexity. By caching the results during object initialization, subsequent calls toget_prompt_variables()
andformat()
become simple attribute lookups instead of string parsing operations.Performance characteristics:
The 5% speedup is most pronounced in scenarios where:
format()
method is called repeatedly on the same prompt instanceThe line profiler shows that while the
get_prompt_variables()
call itself becomes slightly faster (15% vs 13.6% of total time), the overall benefit comes from eliminating redundant parsing work across the entire prompt processing pipeline.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit_tests/utils/test_docs_utils.py::test_prompt_template_token_length
⏪ Replay Tests and Runtime
test_pytest_testsunit_teststest_guard_log_py_testsintegration_teststest_guard_py_testsunit_testsvalidator__replay_test_0.py::test_guardrails_utils_docs_utils_TextSplitter_prompt_template_token_length
To edit these changes
git checkout codeflash/optimize-TextSplitter.prompt_template_token_length-mh1qq7nx
and push.